var baseUrl = "";
// var baseUrl = "http://127.0.0.1:8080";

//ajax分页获取页面内容
function getData(pageNum, dataUrl, pageSize, requestData, method) {
    requestData.size = pageSize;
    requestData.page = pageNum;
    $.ajax({
        type: "GET",
        url: dataUrl,
        data: requestData,
        beforeSend: function (XMLHttpRequest) {
        },
        success: function (result) {
            if (result != "" && result != null) {
                method(result, requestData, pageNum);
            }
        },
        complete: function (XMLHttpRequest, textStatus) {
        },
        error: function () {
        }
    });
}

function page(data, dataUrl, pageSize, pageNum, requestData, method) {
    loadPage(dataUrl, data.totalPages, pageSize, pageNum, requestData, method);
}

// 分页
function loadPage(dataUrl, totalPages, pageSize, pageNum, requestData, method) {
    var htmls = "<ul class='pagination pagination-sm'>";
    if (parseInt(pageNum) + 1 == 1) {
        htmls += "<li><a href='#' aria-label='Previous'><span aria-hidden='true'>&laquo;</span></a></li>";
    } else {
        htmls += '<li><a href="javasist:;" onclick=getData("' + (parseInt(pageNum) - 1) + '","' + dataUrl + '","' + pageSize + '",' + JSON.stringify(requestData) + ',' + method + ') aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>';
    }

    if (totalPages < 7) {
        for (var i = 1; i <= totalPages; i++) {
            if (parseInt(pageNum) + 1 == i) {
                htmls += "<li class='active'><a href='#'>" + i + "</a></li>";
            } else {
                htmls += '<li><a href="javasist:;" onclick=getData("' + (i - 1) + '","' + dataUrl + '","' + pageSize + '",' + JSON.stringify(requestData) + ',' + method + ')>' + i + '</a></li>';
            }
        }
    } else {
        if (parseInt(pageNum) < 3) {
            for (var i = 1; i <= 5; i++) {
                if (parseInt(pageNum) + 1 == i) {
                    htmls += "<li class='active'><a href='#'>" + i + "</a></li>";
                } else {
                    htmls += '<li><a href="javasist:;" onclick=getData("' + (i - 1) + '","' + dataUrl + '","' + pageSize + '",' + JSON.stringify(requestData) + ',' + method + ')>' + i + '</a></li>';
                }
            }
            htmls += "<li><a>...</a></li>";
            htmls += '<li><a href="javasist:;" onclick=getData("' + (totalPages - 1) + '","' + dataUrl + '","' + pageSize + '",' + JSON.stringify(requestData) + ',' + method + ')>' + totalPages + '</a></li>';
        } else if (parseInt(pageNum) + 5 >= totalPages) {
            for (var i = totalPages - 4; i <= totalPages; i++) {
                if (parseInt(pageNum) + 1 == i) {
                    htmls += "<li class='active'><a href='#'>" + i + "</a></li>";
                } else {
                    htmls += '<li><a href="javasist:;" onclick=getData("' + (i - 1) + '","' + dataUrl + '","' + pageSize + '",' + JSON.stringify(requestData) + ',' + method + ')>' + i + '</a></li>';
                }
            }
        } else {
            for (var i = parseInt(pageNum) - 1; i <= parseInt(pageNum) + 3; i++) {
                if (parseInt(pageNum) + 1 == i) {
                    htmls += "<li class='active'><a href='#'>" + i + "</a></li>";
                } else {
                    htmls += '<li><a href="javasist:;" onclick=getData("' + (i - 1) + '","' + dataUrl + '","' + pageSize + '",' + JSON.stringify(requestData) + ',' + method + ')>' + i + '</a></li>';
                }
            }
            htmls += "<li><a>...</a></li>";
            htmls += '<li><a href="javasist:;" onclick=getData("' + (totalPages - 1) + '","' + dataUrl + '","' + pageSize + '",' + JSON.stringify(requestData) + ',' + method + ')>' + totalPages + '</a></li>';
        }
    }

    if (parseInt(pageNum) + 1 >= totalPages) {
        htmls += "<li><a href='#' aria-label='Next'><span aria-hidden='true'>&raquo;</span></a></li>";
    } else {
        htmls += '<li><a href="javasist:;" onclick=getData("' + (parseInt(pageNum) + 1) + '","' + dataUrl + '","' + pageSize + '",' + JSON.stringify(requestData) + ',' + method + ') aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>';
    }
    $("#thePage").html(htmls);
}

function getDate(date, rule) {
    var date = new Date(date);
    var dateStr = date.pattern(rule);
    return dateStr;
}

Date.prototype.pattern = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, // 月份
        "d+": this.getDate(), // 日
        "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, // 小时
        "H+": this.getHours(), // 小时
        "m+": this.getMinutes(), // 分
        "s+": this.getSeconds(), // 秒
        "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
        "S": this.getMilliseconds()
        // 毫秒
    };
    var week = {
        "0": "/u65e5",
        "1": "/u4e00",
        "2": "/u4e8c",
        "3": "/u4e09",
        "4": "/u56db",
        "5": "/u4e94",
        "6": "/u516d"
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
            .substr(4 - RegExp.$1.length));
    }
    if (/(E+)/.test(fmt)) {
        fmt = fmt
            .replace(
                RegExp.$1,
                ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f"
                        : "/u5468")
                    : "")
                + week[this.getDay() + ""]);
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k])
                : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
}

var oldVal = "";
$(function () {
    // pageSize监听
    $("#select-small").change(function () {
        pageSize = parseInt($(this).children('option:selected').text());// 这就是selected的值
        getData(0, dataUrl);
    });
    // input监听事件
    $('#search').bind('input propertychange', function () {
        if ($.trim($(this).val()) == oldVal) {
            getData(0, dataUrl);
            return;
        }
        oldVal = $.trim($(this).val());
        if ($(this).val().length > 0) {
            requestData.keyword = oldVal;
            getData(0, searchKeyWordUrl);
        }
    });
})

// 下拉框注册监听
function registSelect(id) {
    $("#" + id).change(function () {
        var name = $(this).attr("name");
        var value = $(this).children('option:selected').text();
        eval('(' + "requestData." + name + "=\"" + value + '\")');
        getData(0, dataUrl);
    });
}

// 通用ajax请求 万能类
function commonAjax(url, requestData, methodName, requestType, sendData) {
    $.ajax({
        type: requestType,
        url: url,
        data: requestData,
        beforeSend: function (XMLHttpRequest) {
        },
        success: function (result) {
            if (result != "" && result != null) {
                if (sendData == null && sendData == "") {
                    var str = JSON.stringify(result);
                    eval('(' + methodName + '(' + str + '))');
                } else {
                    var str = JSON.stringify(result);
                    var str1 = JSON.stringify(sendData);
                    eval('(' + methodName + '(' + str + ',' + str1 + '))');
                }
            } else if (result == 0) {
                var str1 = JSON.stringify(sendData);
                eval('(' + methodName + '(' + 0 + ',' + str1 + '))');
            }
        },
        complete: function (XMLHttpRequest, textStatus) {
        },
        error: function () {
        }
    });
}

function changeTime(timeStr) {
    var date = new Date(timeStr);
    var dateYear = date.getFullYear();
    var dateMonth = date.getMonth() + 1;
    var dateDay = date.getDate();
    var hour = date.getHours();
    var min = date.getMinutes();
    // var dateStr = dateYear + "年" + dateMonth + "月" + dateDay + "日 " + hour + ":" + min;
    var dateStr = dateYear + "-" + dateMonth + "-" + dateDay;
    // var dateStr = dateYear + "-" + dateMonth;
    return dateStr;
}//转换时间
